<?php

namespace app\admin\model\wallet;

use think\Model;
use think\Db;
use app\admin\model\Wallet;

class Record extends Model
{

    // 表名
    protected $name = 'wallet_record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_status_text',
        'pay_time_text',
        'type_name'
    ];

    public function user()
    {
        return $this->hasOne('app\admin\model\User','id','user_id')->setEagerlyType(0);
    }

    public function log()
    {
        return $this->hasOne('app\admin\model\wallet\Log','record_sn','record_sn');
    }

    public function wallet()
    {
        return $this->hasOne('app\admin\model\Wallet','user_id','user_id')->setEagerlyType(0);
    }
    public function operationA()
    {
        return $this->hasOne('app\admin\model\Admin','id','operation');
    }

    public function multiple()
    {
        return $this->hasOne('app\admin\model\WaterMultiple','record_sn','record_sn');
    }
    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '1' => __('Pay_status 1'), '-1' => __('Pay_status -1')];
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeNameAttr($value, $data)
    {
        switch ($data['type']){
            case 1:
                return '充值';
                break;
            case 2:
                return '提现';
                break;
            case 3:
                return '扣除';
                break;
        }
    }

    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public static function tixianFalse($data,$operation)
    {
        $record  = self::get($data['id']);
        $wallet  = Wallet::where('user_id',$record['user_id'])->find();

        if( $record['pay_status'] != 0 ){ //审核已通过不允许再次审核
            return ['code'=>0,'msg'=>'不允许重复操作！'];
        }

        Db::startTrans();
        try{
            $record->remark = $data['remark'];
            $record->operation  = $operation;
            $record->pay_status = -1;
            $record->save();

            if( $record->pay_status == -1 ){//只有待审核才进行返款
                $wallet->lock_money = $wallet['lock_money'] - $record['money'];
                $wallet->money      = $wallet['money'] + $record['money'];
                $wallet->save();
            }

            Log::insert([
                'record_sn' => $record['record_sn'],
                'user_id'   => $record['user_id'],
                'change_money'=> '+'.$record['money'],
                'money'     => $wallet['money'],
                'remark'    => $data['remark'],
                'createtime'=> time(),
                'index'     => $record['index']
            ]);
            Db::commit();
            return ['code'=>200,'msg'=>'成功','data'=>[]];

        }catch (\Exception $e){
            Db::rollback();
            return ['code'=>0,'msg'=>'失败','data'=>$e->getMessage()];
        }
    }

    public static function tixianTrue($data,$operation)
    {
        $record = self::get($data['id']);
        $wallet = Wallet::where('user_id',$record['user_id'])->find();

        if( $record['pay_status'] != 0 ){ //审核已通过不允许再次审核
            return ['code'=>0,'msg'=>'不允许重复操作！'];
        }

        Db::startTrans();
        try{

            if( $record->pay_status == 0 || $record->pay_status == -1 ){//只有待审核，或者审核失败才进行lock_money扣除
                $wallet->lock_money = $wallet['lock_money'] - $record['money'];
                $wallet->save();
            }
            $record->pay_status = 1;
            $record->operation  = $operation;
            $record->save();
            Db::commit();
            return ['code'=>200,'msg'=>'成功','data'=>[]];

        }catch (\Exception $e){
            Db::rollback();
            return ['code'=>0,'msg'=>'失败','data'=>$e->getMessage()];
        }
    }

}
